<template>
  <a-select
    v-model:value="yard_id"
    :options="dataSource"
    :replace-fields="replaceFields"
    placeholder="选择小区"
    style="width: 160px"
    @change="change"
  />
</template>

<script>
// import Selector from '@gis/lib/components/selector';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';
export default {
  name: 'yard-selector',
  // components: { Selector },
  props: {
    value: { type: String, default: undefined },
  },
  data() {
    return {
      yard_id: undefined,
      dataSource: [],
      replaceFields: {
        label: 'yard_name',
        value: 'yard_id',
      },
    };
  },
  computed: {
    area_code() {
      const { area_code } = useUserInfoStore();
      return area_code;
    },
  },
  watch: {
    value: {
      immediate: true,
      handler(value) {
        this.yard_id = value;
      },
    },
    area_code: {
      immediate: true,
      handler(code) {
        if (code) this.getDataAsync();
      },
    },
  },
  methods: {
    async getDataAsync() {
      const res = await this.$axios.get(`/xw-microservice/xw-actual-data/v3/house/yard/select/${this.area_code}`);
      this.dataSource = res && res.data ? res.data : [];
    },
    change(val) {
      this.$emit('update:modelValue', val);
      this.$emit('input', val);
    },
  },
};
</script>

<style scoped></style>
